package com.administrator.scoreManageSys.service.impl;

import com.administrator.scoreManageSys.entity.system.User;
import com.administrator.scoreManageSys.mapper.student.StudentExamMangeMapper;
import com.administrator.scoreManageSys.service.StudentExamMangeService;
import com.administrator.scoreManageSys.utils.ResultUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zsh
 * @date 2022/04/12/ 11:36
 */
@Service
public class StudentExamMangeServiceImpl implements StudentExamMangeService {
    @Resource
    private ResultUtils resultUtils;
    @Resource
    private StudentExamMangeMapper studentExamMangeMapper;

    /**
     * 获取教师自己的考试列表
     *
     * @param userId 教师id
     * @param role   角色
     * @return 考试列表
     */
    @Override
    public JSONObject obtainMyOwnExamList(Long userId, Long role) {
       List<Class> result= studentExamMangeMapper.obtainMyOwnExamList(role);
        return resultUtils.resultSuccessData(result);
    }

    /**
     * 给学生打分
     *
     * @param userId      教师id
     * @param studentId   学生id
     * @param season      考季
     * @param subject     科目
     * @param examination 考试
     * @param score       得分
     * @return 操作结果
     */
    @Override
    public JSONObject gradeStudents(Long userId, Long studentId, Long season, Long subject, Long examination, Double score) {
        studentExamMangeMapper.gradeStudents(userId, studentId, season, subject, examination, score);
        return resultUtils.resultSuccess();
    }

    /**
     * 获取学生成绩列表
     *
     * @param userId      教师id
     * @param studentName 学生姓名
     * @param season      考季id
     * @param classId     班级id
     * @param roleId      角色
     * @return 学生成绩列表
     */
    @Override
    public JSONObject getStudentExamList(Long userId, Long studentName, Long season, Long classId, Long roleId) {
        //查看当前教师任课多少班 是否是班主任  任班主任班级是几班
        JSONArray teacherClass = studentExamMangeMapper.getTeacherClass(userId);
        //任课班级
        StringBuilder courseClass = new StringBuilder();
        //班主任班级 如果该老师不是班主任 则该值为 ''
        StringBuilder headTeacher = new StringBuilder();
        for (Object aClass : teacherClass) {
            JSONObject jsonObject = JSON.parseObject(aClass.toString());
            //任课班级
            Long classIds = jsonObject.getLong("classId");
            //班主任班级 没有则为0
            Long classTeacher = jsonObject.getLong("ClassTeacher");
            courseClass.append(classIds).append(",");
            if (classTeacher > 0) {
                headTeacher.append(classTeacher);
            }
        }
        String[] courseClassSplit = courseClass.toString().split(",");
        String headTeacherString = headTeacher.toString();
        List<User> userList = new ArrayList<>();
        List<String> list = Arrays.asList(courseClassSplit);
        list = new ArrayList<>(list);
        list.remove("");
        if (list.size() > 0) {
            //根据班级 班主任获取所有学生信息
            userList = studentExamMangeMapper.getStudentExamList(season, classId, studentName, courseClassSplit, headTeacherString);
        } else if (roleId == 1) {
            //根据班级 班主任获取所有学生信息
            userList = studentExamMangeMapper.getStudentExamList(season, classId, studentName, new String[]{}, headTeacherString);
        } else {
            return resultUtils.resultSuccessData(userList);
        }
        for (User user : userList) {
            if (roleId == 1 || user.getClassId().toString().equals(headTeacherString)) {
                user.setIsOperating(1L);
            } else {
                user.setIsOperating(0L);
            }
        }
        return resultUtils.resultSuccessData(userList);
    }

    /**
     * 修改成绩
     *
     * @param userId      老师id
     * @param roleId      角色
     * @param studentId   学生id
     * @param examination 考试id
     * @param score       成绩
     * @return 操作结果
     */
    @Override
    public JSONObject modifyGrades(Long userId, Long roleId, Long studentId, Long examination, Double score) {
        studentExamMangeMapper.modifyGrades(studentId,examination,score);
        return resultUtils.resultSuccess();
    }
}
